package com.hjb.service.impl.siteuseraccount;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hjb.service.config.constant.Constants;
import com.hjb.service.domain.siteuser.SiteUserExperience;
import com.hjb.service.domain.siteuser.SiteUserExperienceExample;
import com.hjb.service.mapper.SiteUserAccountMapper;
import com.hjb.service.mapper.SiteUserExperienceMapper;
import com.hjb.service.siteuseraccount.SiteUserExperienceService;
import com.hjb.service.user.SiteSysAccountService;
import com.hjb.service.utils.HJBUT;

/**
 * 体验金 接口业务实现类
 * @author zhangxl
 * @date 2017-08-08
 * @version 1.0
 */
@Service
public class SiteUserExperienceServiceImpl implements SiteUserExperienceService{
	
	private Logger log = LoggerFactory.getLogger(getClass());
	
	@Autowired
	private SiteUserExperienceMapper userExperienceMapper;
	@Autowired
	private SiteUserAccountMapper siteUserAccountMapper;
	@Autowired
	private SiteSysAccountService siteSysAccountService;

	@Override
	public BigDecimal queryExperienceBidAmount(Map<String, Object> params) {
		log.info("查询体验金标投资金额:user_id:"+params.get("userId")+",产品id:"+params.get("productId"));
		return userExperienceMapper.queryExperienceBidAmount(params);
	}

	@Override
	@Transactional
	public void transferExperienceIncome() {
		try {
			log.info("体验金投资收益发放开始:........................."+HJBUT.currentTime());
			SiteUserExperienceExample userExperienceExample = new SiteUserExperienceExample();
			userExperienceExample.createCriteria().andStatusEqualTo(Constants.COMMON_STATUS_0).andCashReturnDateEqualTo(HJBUT.currentDate());
			List<SiteUserExperience> userExperienceList = userExperienceMapper.selectByExample(userExperienceExample);
			log.info("体验金投资收益发放任务数:........................."+userExperienceList.size());
			for(SiteUserExperience userExperience:userExperienceList){
				String desc = "体验金投资收益发放详情：user_id:"+userExperience.getUserId()+",体验金投资金额："+userExperience.getAmount()+
						",计息日："+userExperience.getInterestDate()+",收益发放日期:"+userExperience.getCashReturnDate()+",收益金额:"+userExperience.getReturnAmount();
				log.info(desc);
				
				//更新体验金收款任务
				SiteUserExperience updateExperience = new SiteUserExperience();
				updateExperience.setId(userExperience.getId());
				updateExperience.setStatus(Constants.COMMON_STATUS_1);
				updateExperience.setUpdateTime(new Date());
				updateExperience.setReturnDate(HJBUT.currentDate());
				userExperienceMapper.updateByPrimaryKeySelective(updateExperience);
				
				//发放体验金收益
				Map<String,Object> params = new HashMap<String, Object>(); 
				params.put("userId", userExperience.getUserId());
				params.put("amount", userExperience.getReturnAmount());
				siteUserAccountMapper.updateAccountByExperience(params);
				
				String inMsg = "体验金收益金额:"+userExperience.getReturnAmount();
				//系统体验金账户记账
				siteSysAccountService.updateBanlanceByUserId(Constants.SYS_ACCOUNT_10006, userExperience.getReturnAmount(), BigDecimal.ZERO,
						Constants.ACCOUNT_LOG_TYPE_6, userExperience.getUserId(), Constants.SYS_ACCOUNT_10006, String.valueOf(userExperience.getId()), "", "", desc, inMsg, inMsg);
			}
			
			log.info("体验金投资收益发放结束:........................."+HJBUT.currentTime());
		} catch (Exception e) {
			log.info("体验金投资收益发放异常:........................."+HJBUT.currentTime());
		}
		
	}

}
